<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    .router-link-active{
      color:red;
    }
  </style>
</head>
<body>
  <div id="app">
    <router-link tag="button" to="/home">到首页</router-link>
    <router-link tag="button" to="/news/2">到新闻</router-link>
    <router-link replace tag="button" to="/about">到关于我们</router-link>
    <hr>
    <!-- 视图上定义 路由组件出口 -->
    <router-view></router-view>
  </div>
  <script src="./vue.js"></script>
  <script src="./vue-router.js"></script>
  <script>
    // 第一步 组件 路由组件的 options 
    const Home = {
      template: `
        <div>
          <h2>首页</h2>  
        </div>
      `
    }
    const News = {
      template: `
        <div>
          <h2>新闻页</h2>  
        </div>
      `,
      beforeRouteEnter(to,from,next){
       if(confirm('您年满18周岁了吗')){
         next();
       }
      },
      beforeRouteUpdate(to, from, next){
        console.log(to);
        console.log(from);
        next();
      },
      beforeRouteLeave(to, from,next){
        if(confirm('您确认要残忍抛弃我嘛?')){
          next();
        }
      }
    }
    const About = {
      template: `
        <div>
          <h2>关于我们页</h2>  
        </div>
      `
    }
    // 定义404组件 路由 options
    const NotFound = {
      template: `
        <div>
          <h2>404啦</h2>  
        </div>
      `
    }
    // 定义路由 router 实例对象
    const routes = [
      {
        path: '/',
        redirect: '/home'
      },
      {
        path: '/home',
        name:'首页',
        component: Home
      },
      {
        path: '/news/:id',
        name:'新闻',
        component: News
      },
      {
        path: '/about',
        name: '关于我们',
        component: About,
      },
      {
        path: '*',
        component: NotFound
      }
    ]
    const router = new VueRouter({
      routes
    })
   
    const vm = new Vue({
      el: '#app',
      router
    })
  </script>
</body>
</html>